Skip to content

Feature/hw09 state machine, ballistic table, smart pointers - #9

Merged
alex-popkov merged 15 commits into
mainfrom
feature/hw09-state-machine-ballistic-table
Jul 2, 2026
Merged

alex-popkov merged 15 commits into
mainfrom
feature/hw09-state-machine-ballistic-table

Conversation

@alex-popkov

Copy link
Copy Markdown
Owner
  • Smart Pointers: Replaced all raw new/delete with std::unique_ptr and std::move across the codebase — factories, MissionProcessor constructor, init(), and changeSolver()
  • State Machine: Replaced switch/case on DroneState enum with State pattern — 5 state classes (StateStopped, StateAccelerating, StateDecelerating, StateTurning, StateMoving) implementing IDroneState interface. Removed enum DroneState entirely; each state class exposes a static constexpr NAME and a name() method returning std::string
  • Table Solver: Implemented TableSolver using 5D multilinear interpolation (BallisticTable) over a pre-computed ballistic table (altitude, speed, mass, drag, lift). Added SolverType::TABLE to the factory
  • Refactoring:
    Extracted evaluateTarget() and getSimulationStep() private methods from MissionProcessor::step()
    Removed DroneMotion struct, inlined its fields into SimulationStep with drone prefix (droneSpeed, droneDirection, dronePosition, droneState)
    Reordered SimulationStep fields for better memory alignment
    Introduced DroneContext struct with const DroneConfig* pointer for shared state between state classes
    Moved variable declarations inside try block in main.cpp (no manual cleanup needed with RAII)
    Added CLI argument for solver type selection (analytical / table) and ballistic table path
    Updated ammo parameters in ammo.json (VOG-17, M67, RKG-3, GLIDING-VOG, GLIDING-RKG)

@alex-popkov
alex-popkov requested a review from farnblack June 13, 2026 10:43
…github.com/alex-popkov/rd_cpp_docker into feature/hw09-state-machine-ballistic-table

# Conflicts:
#	drone_simulation/src/mission_processor.cpp

@farnblack farnblack left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ДЗ9 — код-рев'ю

src/mission_processor.cpp (весь step/evaluateTarget/getSimulationStep) — (ДЗ9-deliverable: TableSolver не працює)

// init():
this->ammoFlightTime = getAmmoFlightTime(this->droneConfig, this->ammo);
this->ammoHorizontalFlightDistance = getAmmoHorizontalFlightDistance(this->droneConfig.attackSpeed, this->ammoFlightTime, this->ammo);
// step()/evaluateTarget()/getSimulationStep() далі користуються лише цими двома полями.
// this->ballisticSolver НЕ викликається ніде, окрім конструктора і changeSolver().

TableSolver::solve() реалізований правильно (справжня 5D-інтерполяція у BallisticTable::lookup), але жодного разу не викликається. Балістику в симуляції рахують вільні функції getAmmoFlightTime / getAmmoHorizontalFlightDistance (аналітичні формули Кардано з simulation.cpp), а об'єкт ballisticSolver лише зберігається в полі та ніде не використовується. Перевірив прогоном: запуск з аргументом analytical і з аргументом table дає побайтово однаковий simulation.json — солвер мертвий, перемикач не впливає ні на що. Головне завдання ДЗ9 (підключити табличний солвер у симуляцію) не виконане.

src/mission_processor.cpp + include/interfaces/ballistic_solver.hpp — (DIP / абстракція декоративна)

// MissionProcessor зберігає std::unique_ptr<IBallisticSolver> ballisticSolver,
// але вся балістика йде повз інтерфейс — через глобальні вільні функції.

Оскільки балістика йде повз солвер, інтерфейс IBallisticSolver і фабрика солверів існують лише формально. Поліморфізм не задіяний — фабрика створює об'єкт, який нікуди не приєднаний. Балістику слід рахувати через ballisticSolver->solve(...), а не дублювати у вільних функціях.

src/main.cpp:41-46 — (немає перевірки на nullptr)

auto ballisticSolver = createSolver(solverType, droneConfig, ballisticTablePath);
// ... createSolver / createProvider / createLoader можуть повернути nullptr,
MissionProcessor missionProcessor(std::move(targets), std::move(ballisticSolver));
missionProcessor.init(std::move(loader));

Усі три фабричні функції (factory.cpp) повертають nullptr для невідомого типу, але результат не перевіряється. Звертання до nullptr-компонента = UB. Перед використанням перевіряти кожен.

src/simulation.cpp:10 (#define ENABLE_LOG 1) + src/main.cpp:13 — (дебаг-вивід лишився ввімкненим)

#define ENABLE_LOG	1
// ... LOG(...) друкує у stdout, у т.ч. "No valid target at this step, skipping" та "Drone hit the target..."

Логування лишилось увімкненим у фінальній здачі — симуляція засмічує stdout. Для здачі вимкнути.

@alex-popkov
alex-popkov merged commit a99a6c6 into main Jul 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants